import frame from '@/views/frame.vue';
import { createRouter, createWebHashHistory } from 'vue-router';

export const menus = [
  {
    path: "webCategory",
    name: "webCategory",
    meta: {
      title: "网站"
    },
    component: () => import('@/views/webCategory.vue')
  },
  {
    path: "searchCategory",
    name: "searchCategory",
    meta: {
      title: "搜索引擎"
    },
    component: () => import('@/views/searchCategory.vue')
  },
]

const routes = [
  {
    path: '/',
    name: 'login',
    label: "login",
    meta: {
      title: "登录"
    },
    component: () => import('@/views/login.vue')
  },
  {
    path: '/main',
    name: 'frame',
    component: frame,
    children: menus
  },
]

const router = createRouter({
  history: createWebHashHistory("/backstage"),
  routes: routes
})

// 路由钩子设置
router.beforeEach((to, from, next) => {
  document.title = to.meta.title
  let _token = localStorage.getItem('local:_token')
  let _token_expired = localStorage.getItem('_token_expired')
  let now = Date.parse(new Date()) / 1000
  if (to.name == 'notFound' || to.name == 'login') {
    next()
  } else if (!_token) {
    next({ name: 'login' })
  } else if (now > _token_expired) {
    localStorage.removeItem('local:_token')
    localStorage.removeItem('_token_expired')
    next({ name: 'login' })
  } else {
    next()
  }
})

export default router
